package com.lxh.lxhcrm.controller;

import com.lxh.lxhcrm.model.pojos.Department;
import com.lxh.lxhcrm.model.pojos.FunctionType;
import com.lxh.lxhcrm.model.pojos.Position;
import com.lxh.lxhcrm.model.pojos.Role;
import com.alibaba.fastjson.JSONObject;
import com.lxh.lxhcrm.model.service.DeptService;
import com.lxh.lxhcrm.model.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开发团队 ：lxh
 * 开发人员 ：蒋鑫
 * 开发时间 ：2020/1/10 15：33
 * 类的名称 ：DeptController
 * 类的描述 ：权限模块DeptController，进行前后端的数据传输控制
 */
@Controller
public class DeptController {
    @Autowired
    DeptService deptService;
    EmpService empService;
    /*查询所有部门*/
    @RequestMapping("/dept-list")
    @ResponseBody
    public List<Department> findAllDept(){
        return deptService.findAllDept();
    };

    /*点击查询部门的角色*/
    @RequestMapping("/role-list")
    @ResponseBody
    public  List<Role> findRole(int departmentId){
        return deptService.findRole(departmentId);
    }
    /*查询正常的所有部门*/
    @RequestMapping("/dept-alllist")
    @ResponseBody
    public List<Department> findDept(){
        return deptService.findDept();
    };

    /*根据部门id 查询部门下的职位*/
    @RequestMapping("/post-list")
    @ResponseBody
    public List<Position> findPost( Integer departmentId){
        return deptService.findPost(departmentId);
    };





    /*查询所点击部门的权限组id集合*/
    /*查询所有的权限组*/
    @RequestMapping("/funType-list")
    @ResponseBody
    public Map<String,Object> findFunType(Integer departmentId){
        Map<String,Object> map = new HashMap<>();
        /*查询所有的权限组*/
        List<FunctionType> funTypeList = deptService.allFunctionType();
        /*查询部门已有的权限组*/
        List<Integer> deptFunType = deptService.findFunType(departmentId);
        map.put("funTypeList",funTypeList);
        map.put("deptFunType",deptFunType);
        System.out.println(departmentId+"departmentId");
        System.out.println(funTypeList+"funTypeList");
        System.out.println(deptFunType+"deptFunType");
        return map;
    }

/*保存部门权限组*/
    @RequestMapping("/save-privilege")
    @ResponseBody
    public void savePrivilege(@RequestParam ("str") String str){
        System.out.println("保存部门权限组");
        JSONObject parse = (JSONObject)JSONObject.parse(str);
        Integer deptId = (Integer)parse.get("departmentId");
        List<Integer> funTypeId = (List<Integer>)parse.get("funTypeId");
        System.out.println("保存开始");
        System.out.println(deptId+"deptIdsss");
        System.out.println(str+"str");
        System.out.println(funTypeId+"funTypeIdsss");
        deptService.savePrivilege(deptId,funTypeId);
    }

    /*根据权限组id  查询权限组 名字 id*/
    @RequestMapping("/ftname-list")
    @ResponseBody
    public  List<FunctionType> findFtName(@RequestParam ("str") String str){
        JSONObject parse = (JSONObject)JSONObject.parse(str);
        List<Integer> funTypeId=(List<Integer>)parse.get("funTypeId");

        Integer[] array = new Integer[funTypeId.size()];
        // List转换成数组
        for (int i = 0; i < funTypeId.size(); i++) {
            array[i] = funTypeId.get(i);
        }
        String id="(";
        // 输出数组
        for (int i = 0; i < array.length; i++) {
            System.out.println("array--> " + array[i]);
            if(array[i]==101){}
            else{
                id=id+array[i]+",";
                System.out.println(id);}
        }
       id= id.substring(0,id.length()-1);
            id=id+")";
            return  deptService.findFtName(id);
    }
    /**
     * 查询所有部门及其下面得员工
     * @author：鞠玮
     * @return List<Department>:部门对象
     */
    @GetMapping("dept_emps")
    @ResponseBody
    public List<Department> findAllDeptAndEmp(){
        return deptService.findAllDeptAndEmp();
    }

}
